(function ($) {
    "use strict";

    let $dom = {
        $tableContainer: $("#table-container"),
        $addBtn: $("#add-btn")
    };

    /**
     * 添加文档
     */
    $dom.$addBtn.on("click",function () {
        window.top.changeIframe("/admin/frame/addDoc");
    });

    /**
     * 查询文档信息
     */
    function queryDocInfo() {
        $.ajax({
            url: "/admin/frame/queryAllDoc",
            type: "post",
            dataType: "json",
            success: function (resultD) {
                if( (typeof resultD)==="object" ){
                    let status = resultD.status;

                    if(status === 200){
                        let info = resultD.info;

                        if( (typeof info)==="string" ){
                            info = JSON.parse(info);
                        }

                        let data = {
                            commonArray: [],
                            studentArray: [],
                            teacherArray: [],
                            parentsArray: []
                        };

                        for(let i=0,len=info.length; i<len; i++){
                            let role = info[i].role;

                            if(role === 0){
                                info[i].area = "公共文档";
                                data.commonArray.push(info[i]);
                            }else if(role === 1){
                                info[i].area = "教师文档";
                                data.teacherArray.push(info[i]);
                            }else if(role === 2){
                                info[i].area = "家长文档";
                                data.parentsArray.push(info[i]);
                            }else {
                                info[i].area = "学生文档";
                                data.studentArray.push(info[i]);
                            }
                        }

                        doOutput(data);
                    }
                }
            }
        });
    }

    /**
     * 遍历输出
     */
    function doOutput(docArray) {

        let tableHead = "<div class=\"row\">"+
                            "<div class=\"table-responsive\">"+
                                "<table class=\"table table-bordered table-hover table-condensed\">";
        let tHead = "<thead>"+
                        "<tr>"+
                            "<th>序号</th>"+
                            "<th>标题</th>"+
                            "<th>添加时间</th>"+
                            "<th>上一次修改时间</th>"+
                            "<th>排序序号</th>"+
                            "<th>是否启用</th>"+
                            "<th>操作</th>"+
                        "</tr>"+
                    "</thead>";
        let tableTail = "</table>"+
                    "</div>"+
                "</div>";

        for(let key in docArray){
            let tableBody = "<tbody>";
            for(let j=0,lenJ=docArray[key].length; j<lenJ; j++){
                let isEnable = docArray[key][j].isEnable === 1 ? "是" : "否";
                let tr = "<tr>"+
                            "<td>"+ (parseInt(j)+1) +"</td>"+
                            "<td>"+ titleSub(docArray[key][j].title) +"</td>"+
                            "<td>"+ formatTime(docArray[key][j].addTime) +"</td>"+
                            "<td>"+ formatTime(docArray[key][j].alterTime) +"</td>"+
                            "<td>"+ docArray[key][j].order +"</td>"+
                            "<td>"+ isEnable +"</td>"+
                            "<td>"+
                                "<button data-id=\""+ docArray[key][j].id +"\" name=\"detail-btn\" type=\"button\" class=\"btn btn-default\">详情</button>"+
                                "<button data-id=\""+ docArray[key][j].id +"\" name=\"manager-btn\" type=\"button\" class=\"btn btn-default\">管理</button>"+
                            "</td>"+
                        "</tr>";
                tableBody = tableBody + tr;
            }

            if(docArray[key].length > 0){
                tableBody = tableBody + "</tbody>";

                let caption = "<caption style=\"font-size: 16px;color: #000;\">"+ docArray[key][0].area +"</caption>";
                let tableContent = tableHead + caption + tHead + tableBody + tableTail;
                $dom.$tableContainer.append(tableContent);
            }
        }

        updateDoc();
    }

    /**
     * 绑定点击事件
     * 修改文档
     */
    function updateDoc() {
        $("[name='manager-btn']").on("click",function (ev) {
            let event = ev||window.event;
            let target = event.target || event.srcElement;

            let id = $(target).attr("data-id");
            if(id !== ""){
                window.top.changeIframe("/admin/frame/update-doc-page?docId="+id);
            }
        });
    }

    /**
     * 时间格式化
     */
    function formatTime(longTime){
        if(longTime === null || longTime === ""){
            return longTime;
        }

        let date = new Date(longTime);

        let year = date.getFullYear(),
            month = date.getMonth()+1,
            day = date.getDate(),
            hour = date.getHours(),
            minute = date.getMinutes(),
            second = date.getSeconds();

        month = month < 10 ? "0"+month : month;
        day = day < 10 ? "0"+day : day;
        hour = hour < 10 ? "0"+hour : hour;
        minute = minute < 10 ? "0"+minute : minute;
        second = second < 10 ? "0"+second : second;

        return year +"/"+ month +"/"+ day +" "+ hour +":"+minute+":"+second;
    }

    /**
     * 文档标题截取
     */
    function titleSub(titleStr){
        if(titleStr.length > 10){
            return titleStr.substr(0,10);
        }
        return titleStr;
    }

    $(document).ready(function () {
        queryDocInfo();
    });
})(window.jQuery);